package org.yourscrum.web;

import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.yourscrum.service.ThemeService;
import org.yourscrum.web.ThemeVO;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Jettro Coenradie
 */
public class NewThemeController extends SimpleFormController {
    private ThemeService themeService;

    public NewThemeController(ThemeService themeService) {
        this.themeService = themeService;
        setCommandName("theme");
        setCommandClass(ThemeVO.class);
    }

    @Override
    protected Object formBackingObject(HttpServletRequest httpServletRequest) throws Exception {
        return new ThemeVO();
    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
        binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
    }

    @Override
    protected void doSubmitAction(Object command) throws Exception {
        ThemeVO theme = (ThemeVO) command;
        this.themeService.storeTheme(theme);
    }
}
